package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {

    /**
     * 生成订单数据
     * @param orders
     */
    @Insert("insert into orders (number, user_id, address_book_id, order_time, amount, remark, phone, " +
            "address, user_name, consignee, estimated_delivery_time, delivery_status, delivery_time, " +
            "pack_amount, tableware_number, tableware_status)" +
            "values (#{number}, #{userId}, #{addressBookId} , #{orderTime} , #{amount} , #{remark} , #{phone} , " +
            "#{address} , #{userName} , #{consignee} , #{estimatedDeliveryTime} , #{deliveryStatus} , #{deliveryTime} , " +
            "#{packAmount} , #{tablewareNumber} , #{tablewareStatus} )")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert(Orders orders);

    /**
     * 根据订单号查询订单
     * @param orderNumber
     */
    @Select("select * from orders where number = #{orderNumber}")
    Orders getByNumber(String orderNumber);

    /**
     * 修改订单信息
     * @param orders
     */
    void update(Orders orders);

    /**
     * 历史订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    Page<Orders> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO);

    /**
     * 获取订单信息
     * @param id
     * @return
     */
    @Select("select * from orders where id = #{id} ")
    Orders getById(Long id);

    /**
     * 根据查询条件返回订单列表
     * @param pendingPayment
     * @param time
     * @return
     */
    @Select("select * from orders where status = #{status} and order_time < #{time}")
    List<Orders> getListByCondition(Integer status, LocalDateTime time);

    /**
     * 各个状态的订单数量统计(待接单、待派送、派送中)
     * @param status
     * @return
     */
    @Select("select count(*) from orders where status = #{status}")
    Integer getOrderCount(Integer status);

    /**
     * 营业额统计
     * @param map
     * @return
     */
    Double turnoverStat(Map<String, Object> map);

    /**
     * 订单统计
     * @param map
     * @return
     */
    Integer orderStat(Map<String, Object> map);

    /**
     * 销售排名top10
     * @param map
     * @return
     */
    List<GoodsSalesDTO> salesTop10(Map<String, Object> map);
}
